package com.aruba.lifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.SystemClock;
import android.widget.Chronometer;

public class MainActivity extends AppCompatActivity {


    private Chronometer chronometer;
    private long countTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        chronometer = findViewById(R.id.chronometer);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //恢复计时，基于休息的时间作一个偏移
        chronometer.setBase(SystemClock.elapsedRealtime() - countTime);
        chronometer.start();
    }


    @Override
    protected void onPause() {
        super.onPause();
        //记录下计时时间
        countTime = SystemClock.elapsedRealtime() - chronometer.getBase();
        chronometer.stop();
    }
}